package org.zju.demo.shoppingdemo.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.zju.demo.shoppingdemo.R;
import org.zju.demo.shoppingdemo.controller.UserInfoController;
import org.zju.demo.shoppingdemo.model.Request.RegisterModel;
import org.zju.demo.shoppingdemo.model.response.BaseResponse;

/**
 * Created by ZJDX on 2015/9/10.
 */
public class RegisterActivity extends CommonActivity{

    private EditText usernameEditText = null;

    private EditText passwordEditText = null;

    private EditText verifyEditText = null;

    private TextView submitTextView = null;

    private ProgressDialog progressDialog = null;

    private Listener listener = new Listener(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.register);

        super.textViewTitle = (TextView) findViewById(R.id.title);
        super.textViewTitle.setText("注册");

        usernameEditText = (EditText)findViewById(R.id.register_mobile_phone);
        passwordEditText = (EditText)findViewById(R.id.register_password);
        verifyEditText = (EditText) findViewById(R.id.register_repassword);
        submitTextView = (TextView) findViewById(R.id.save_personal_info);

        this.initBottomMenu();
        submitTextView.setOnClickListener(listener);

        progressDialog = new ProgressDialog(this);
    }

    class Listener implements View.OnClickListener {
        private RegisterActivity activity = null;
        public Listener(RegisterActivity activity) {
            this.activity = activity;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.save_personal_info:
                    closeKeyBoard();

                    String username = usernameEditText.getText().toString();
                    String password = passwordEditText.getText().toString();
                    String confirm = verifyEditText.getText().toString();

                    String showMessage = null;
                    if(username == null || username.equals("")) {
                        showMessage = "用户名不能为空";
                    } else if (password == null || password.equals("")) {
                        showMessage = "密码不能为空";
                    } else if(!confirm.equals(password)) {
                        showMessage = "密码不一致";
                    }
                    if (showMessage != null) {
                        Toast toast = Toast.makeText(activity, showMessage, Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    } else {
                        RegisterModel model = new RegisterModel(username, password);
                        Log.i("Register", "username " + username + "password " + password);
                        UserInfoController.doRegister(activity, model);
                    }
                    break;
                default:
            }
        }
    }

    @Override
    public void onResponseMessage(BaseResponse response) {
        progressDialog.cancel();
        if (response.getResult() == 0) {
            // success
            Toast toast = Toast.makeText(this, response.getMessage(), Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            // 返回上一个Activity: LoginActivity
            this.finish();
        } else {
            // error, show error message
            Toast toast = Toast.makeText(this, response.getMessage(), Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }

    @Override
    public void onPreExecute() {
        progressDialog.show();
    }

    @Override
    public void initBottomMenu() {
        imageViewIndex = (ImageView) findViewById(R.id.menu_home_img);
        imageViewIndex.setOnTouchListener(indexListener);
        imageViewIndex.setImageResource(R.drawable.menu_home_released);

        imageViewShopping = (ImageView) findViewById(R.id.menu_shopping_cart_img);
        imageViewShopping.setOnTouchListener(shoppingListener);
        imageViewShopping.setImageResource(R.drawable.menu_shopping_cart_released);

        imageViewMyInfo = (ImageView) findViewById(R.id.menu_personal_center_image);
//        imageViewMyInfo.setOnTouchListener(myInfoListener);
        imageViewMyInfo.setImageResource(R.drawable.menu_my_info_pressed);
    }

    private void closeKeyBoard() {
        InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}
